import excel "C:\Users\evanl\Desktop\Polarization\Slim VDem Data.xlsx", sheet("Sheet1") firstrow 

missings dropobs v2x_libdem		, force
missings dropobs v2cacamps		, force

tab year

rename country_name country
egen id=group(country)

xtset id year

rename v2x_libdem lib
rename v2cacamps ppol

replace ppol=(ppol/8)+0.5

rename v2x_regime regime

gen libdemtenure=0
replace libdemtenure=year-1993 if id==1
replace libdemtenure=year-1956 if id==5
replace libdemtenure=year-2015 if id==9 
replace libdemtenure=year-2013 if id==12 
replace libdemtenure=year-1976 if id==22 
replace libdemtenure=year-2010 if id==23 
replace libdemtenure=year-1996 if id==25 
replace libdemtenure=year-1991 if id==31
replace libdemtenure=year-1999 if id== 41
replace libdemtenure=year-1950 if id== 48
replace libdemtenure=year-1926 if id== 62
replace libdemtenure=year-1967 if id== 63
replace libdemtenure=year-1970 if id== 64
replace libdemtenure=year-1952 if id== 67
replace libdemtenure=year-1993 if id== 78
replace libdemtenure=year-2000 if id== 88
replace libdemtenure=year-1947 if id== 90
replace libdemtenure=year-1990 if id== 103
replace libdemtenure=year-1977 if id== 104
replace libdemtenure=year-2007 if id== 111
replace libdemtenure=year-1999 if id== 114
replace libdemtenure=year-1991 if id== 115
replace libdemtenure=year-2000 if id== 118
replace libdemtenure=year-1983 if id== 120
replace libdemtenure=year-1922 if id== 123
replace libdemtenure=year-2005 if id== 132
replace libdemtenure=year-1919 if id== 139
replace libdemtenure=year-1986 if id== 140
replace libdemtenure=year-2017 if id== 142

replace libdemtenure=year-1921 if id== 11 & year>=1921 & year<=1924
replace libdemtenure=year-1963 if id== 11 & year>=1963
replace libdemtenure=year-2009 if id== 13 & year>=2009 & year<=2012
replace libdemtenure=year-2016 if id== 13 & year==2016
replace libdemtenure=year-2019 if id== 13 & year==2019
replace libdemtenure=year-2010 if id== 16 & year<=2016
replace libdemtenure=year-2018 if id== 16 & year>=2018
replace libdemtenure=year-1902 if id== 33 & year<=1939
replace libdemtenure=year-1946 if id== 33 & year>=1946
replace libdemtenure=year-2003 if id== 49 & year<=2014
replace libdemtenure=year-2017 if id== 49  & year<=2017
replace libdemtenure=year-1991 if id== 57 & year<=2005
replace libdemtenure=year-2007 if id== 57 & year>=2007 & year<=2009
replace libdemtenure=year-2009 if id== 73 & year<=2015
replace libdemtenure=year-2017 if id== 73 & year>=2017
replace libdemtenure=year-1985 if id== 82 & year<=2016
replace libdemtenure=year-2018 if id== 82  & year==2018
replace libdemtenure=year-1906 if id== 94 & year<=1939
replace libdemtenure=year-1946 if id== 94  & year>=1946
replace libdemtenure=year-1993 if id== 119 & year<=2017
replace libdemtenure=year-2019 if id== 119 & year>=2019
replace libdemtenure=year-2000 if id== 126 & year<=2012
replace libdemtenure=year-2016 if id== 126 & year>=2016
replace libdemtenure=year-2017 if id== 133 & year<=2017
replace libdemtenure=year-2019 if id== 133  & year>=2019

replace libdemtenure=0 if libdemtenure<0

gen demtenure=0

replace demtenure=year-	2002		if id==1 & year<2018
replace demtenure=year-	1920		if id==3 & year<1930
replace demtenure=year-	1964		if id==3 & year>=1964 &year<1966
replace demtenure=year-	1974		if id==3 & year>=1974 & year<=1975
replace demtenure=year-	1984		if id==3 & year>=1984
replace demtenure=year-	1921		if id==5 & year<1933
replace demtenure=year- 1945			if id==8 & year>=1945
replace demtenure=year-	1992		if id==8 & year>=1992 & year<=2001
replace demtenure=year-	2009		if id==8 & year >=2009 & year<=2010
replace demtenure=year-	1991		if id==10 & year<1997
replace demtenure=year-	1900		if id==11 & year<1903
replace demtenure=year-	1904		if id==11 & year==1904 | year==1905
replace demtenure=year-	1910		if id==11 & year>=1910 & year<=1912
replace demtenure=year-	1921		if id==11 & year>=1921 & year<=1924
replace demtenure=year-	1929		if id==11 & year>=1929 & year<=1936
replace demtenure=year-	1947		if id==11 & year>=1947 
replace demtenure=year-	1992		if id==12 & year>=1992 & year<2019
replace demtenure=year-	2009		if id==13 & year>2008
replace demtenure=year-	1985		if id==14 & year>=1985 & year<=2018
replace demtenure=year-	1997		if id==15 & year>=1997
replace demtenure=year-	1967		if id==16 & year>=1967
replace demtenure=year-	1991		if id==18 & year>=1991
replace demtenure=year-	2000		if id==19 & year>=2000 & year<=2014
replace demtenure=year-	2016		if id==19 & year>=2016
replace demtenure=year-	1921		if id==22 & year>=1921
replace demtenure=year-	1991		if id==23 & year>=1991
replace demtenure=year-	1959		if id==25 & year>=1959 & year<=1973
replace demtenure=year-	1990		if id==25 & year>=1990
replace demtenure=year-	1991		if id==27 & year>=1991
replace demtenure=year-	2006		if id==28 & year>=2006 & year<=2014
replace demtenure=year-	2000		if id==29 & year>=2000
replace demtenure=year-	1920		if id==31 & year>=1920 & year<=1938
replace demtenure=year-	1991		if id==31 & year>=1991
replace demtenure=year-	1902		if id==33 & year>=1902 & year<=1942
replace demtenure=year-	1946		if id==33 & year>=1946
replace demtenure=year-	1996		if id==35 & year>=1996
replace demtenure=year-	1999		if id==38 & year>=1999
replace demtenure=year-	1919		if id==41 & year>=1919 & year<=1933
replace demtenure=year-	1990		if id==41 & year==1991
replace demtenure=year-	1993		if id==41 & year>=1993
replace demtenure=year-	1967		if id==44 & year>=1967 & year<=1986
replace demtenure=year- 1993		if id==44 & year>=1993 & year<=1999
replace demtenure=year-	2002		if id==44 & year>=2002 & year<=2006
replace demtenure=year-	2004		if id==47 & year>=2004 & year<=2009
replace demtenure=year-	2011		if id==47 & year>=2011 
replace demtenure=year-	1920		if id==48 & year>=1920 & year<=1932
replace demtenure=year-	1949		if id==48 & year>=1949
replace demtenure=year-	1994		if id==49 & year>=1994
replace demtenure=year-	2000		if id==50 & year>=2000
replace demtenure=year-	2015		if id==52 & year>=2015
replace demtenure=year-	1998		if id==53 & year>=1998
replace demtenure=year-	1990		if id==57 & year>=1990 & year<=2017
replace demtenure=year-	1952		if id==59 & year>=1952 & year<=1974
replace demtenure=year-	1977		if id==59 & year>=1977
replace demtenure=year-	1956		if id==60 & year==1957
replace demtenure=year-	2000		if id==60 & year>=2000
replace demtenure=year-	1922		if id==62 & year>=1922
replace demtenure=year-	1949		if id==63 & year>=1949
replace demtenure=year-	1947		if id==64 & year>=1947
replace demtenure=year-	2013		if id==65 & year>=2013
replace demtenure=year-	1955		if id==66 & year>=1955 & year<=1977
replace demtenure=year-	1984		if id==66 & year>=1984
replace demtenure=year-	1952		if id==67 & year>=1952
replace demtenure=year-	1923		if id==73 & year>=1923 & year<=1933
replace demtenure=year-	1990		if id==73 & year>=1990
replace demtenure=year-	2010		if id==74 & year>=2010 & year<=2013
replace demtenure=year-	2014		if id==74 & year>=2014
replace demtenure=year-	2003		if id==75 & year>=2003
replace demtenure=year-	2006		if id==76 & year>=2006
replace demtenure=year-	1921		if id==78 & year>=1921 & year<=1926
replace demtenure=year-	1990		if id==78 & year>=1990
replace demtenure=year-	1995		if id==79 & year>=1995 & year<=1998
replace demtenure=year-	2009		if id==79 & year>=2009 & year<=2018
replace demtenure=year-	1948		if id==81 & year>=1948 & year<=1957
replace demtenure=year-	1963		if id==81 & year>=1963
replace demtenure=year-	1968		if id==82 & year>=1968
replace demtenure=year-	1995		if id==83 & year>=1995
replace demtenure=year-	1991		if id==84 & year>=1991
replace demtenure=year-	2003		if id==85 & year>=2003 & year<=2007
replace demtenure=year-	2005		if id==87 & year>=2005 & year<=2008
replace demtenure=year-	1990		if id==88 & year>=1990 & year<=1993
replace demtenure=year-	1995		if id==88 & year>=1995 
replace demtenure=year-	2010		if id==89 & year>=2010 & year<=2011
replace demtenure=year-	2014		if id==89 & year>=2014
replace demtenure=year-	1947		if id==90 & year>=1947
replace demtenure=year-	2013		if id==92 & year>=2013 & year<=2018
replace demtenure=year-	1906		if id==94 & year>=1906 & year<=1940
replace demtenure=year-	1946		if id==94 & year>=1946
replace demtenure=year-	2004		if id==98 & year>=2004 & year<=2006
replace demtenure=year-	1991		if id==99 & year>=1991
replace demtenure=year-	1994		if id==100 & year>=1994
replace demtenure=year-	1981		if id==101 & year>=1981 & year<=1991
replace demtenure=year-	2001		if id==101 & year>=2001
replace demtenure=year-	1988		if id==102 & year>=1988 & year<=2003
replace demtenure=year-	2010		if id==102 & year>=2010
replace demtenure=year-	1920		if id==103 & year>=1920 & year<=1925
replace demtenure=year-	1990		if id==103 & year>=1990
replace demtenure=year-	1976		if id==104 & year>=1976
replace demtenure=year-	1991		if id==105 & year>=1991
replace demtenure=year-	1992		if id==106 & year==1993
replace demtenure=year-	1992		if id==108 & year>=1992
replace demtenure=year-	1984		if id==110 & year>=1984 & year<=1987
replace demtenure=year-	1990		if id==110 & year>=1990
replace demtenure=year-	2003		if id==112 & year>=2003 & year<=2011
replace demtenure=year-	2016		if id==112 & year>=2016
replace demtenure=year-	1995		if id==114 & year>=1995
replace demtenure=year-	1991		if id==115 & year>=1991
replace demtenure=year-	1995		if id==118 & year>=1995
replace demtenure=year-	1988		if id==119 & year>=1988
replace demtenure=year-	1932		if id==120 & year>=1932 & year<=1936
replace demtenure=year-	1978		if id==120 & year>=1978
replace demtenure=year-	1970		if id==121 & year>=1970 & year<=1981
replace demtenure=year-	1983		if id==121 & year>=1983 & year<=1985
replace demtenure=year-	1987		if id==121 & year>=1987 & year<=1989
replace demtenure=year-	1993		if id==121 & year>=1993 & year<=2005
replace demtenure=year-	2015		if id==121 & year>=2015
replace demtenure=year-	1922		if id==123 & year>=1922
replace demtenure=year-	1996		if id==126 & year>=1996
replace demtenure=year-	1996		if id==127 & year>=1996 & year<=2000
replace demtenure=year-	2007		if id==127 & year>=2007 & year<=2012
replace demtenure=year-	2014		if id==127 & year>=2014 & year<=2015
replace demtenure=year-	1998		if id==128 & year>=1998 & year<=2005
replace demtenure=year-	2018		if id==129 & year>=2018
replace demtenure=year-	1988		if id==129 & year>=1988 & year<=1989
replace demtenure=year-	2003		if id==130 & year>=2003
replace demtenure=year-	2008		if id==131 & year>=2008 & year<=2009
replace demtenure=year-	2014		if id==131 & year>=2014 & year<=2016
replace demtenure=year-	1960		if id==132 & year>=1960
replace demtenure=year-	2012		if id==133 & year>=2012
replace demtenure=year-	1966		if id==134 & year>=1966 & year<=1979
replace demtenure=year- 1990		if id==134 & year>=1990 & year<=2013
replace demtenure=year-	1994		if id==137 & year>=1994 & year<=1997
replace demtenure=year-	2006		if id==137 & year>=2006 & year<=2011
replace demtenure=year-	1919		if id==139 & year>=1919
replace demtenure=year-	1985		if id==140 & year>=1985
replace demtenure=year-	1939		if id==140 & year>=1939 & year<=1972
replace demtenure=year-	1923		if id==140 & year>=1923 & year<=1932
replace demtenure=year-	1980		if id==142 & year>=1980
replace demtenure=year-	1963		if id==143 & year>=1963 & year<=2002
replace demtenure=year-	2006		if id==146 & year>=2006 & year<=2013
replace demtenure=year-	2000		if id==146 & year>=2000 & year<=2001

replace demtenure=0 if demtenure<0

rename e_migdpgro gdpgrow
rename e_migdppc gdppc
rename e_miinflat inflate

gen gdppcpt=gdppc/1000
label variable gdppcpt "GDP per capita, per thousand"


/////////////////////////////////////////////////////////////////////////////////////////////////
// Polarization Rate Indicator


gen id1=0
replace id1=1 if ppol-L.ppol<-0.01 // decline since previous year 


gen id2=0
replace id2=1 if 		(ppol	<	F.ppol 		& F.ppol	<	F2.ppol)	| ///
								(ppol	<	F.ppol 		& F2.ppol	<	F3.ppol) | ///
								(ppol	<	F.ppol 		& F3.ppol	<	F4.ppol) | ///
								(ppol	<	F.ppol 		& F4.ppol	<	F5.ppol) | ///
								(F.ppol	<	F2.ppol 	& F2.ppol	<	F3.ppol) | ///
								(F.ppol	<	F2.ppol 	& F3.ppol	<	F4.ppol) | ///
								(F.ppol	<	F2.ppol 	& F4.ppol	<	F5.ppol) | ///
								(F2.ppol<	F3.ppol 	& F3.ppol	<	F4.ppol) | ///
								(F2.ppol<	F3.ppol 	& F4.ppol	<	F5.ppol) | ///
								(F3.ppol<	F4.ppol 	& F4.ppol	<	F5.ppol)		// 2 increases in the past 5 years
		
gen id3=0 
replace id3=1 if (ppol-L5.ppol)>=0.1  & L5.ppol!=. // increase by 0.1 within 5 year period
replace id3=1 if (F.ppol-L4.ppol)>=0.1 & F.ppol!=. & L4.ppol!=.
replace id3=1 if (F2.ppol-L3.ppol)>=0.1 & F2.ppol!=. & L3.ppol!=.
replace id3=1 if (F3.ppol-L2.ppol)>=0.1 & F3.ppol!=. & L2.ppol!=.
replace id3=1 if (F4.ppol-L.ppol)>=0.1  & F4.ppol!=. & L.ppol!=.
replace id3=1 if (F5.ppol-ppol)>=0.1  & F5.ppol!=.


gen id4=0   							//increase by 0.1 in the next five years 
replace id4=1 if (F5.ppol-ppol)>=0.1 & F5.ppol!=.
replace id4=1 if (F4.ppol-ppol)>=0.1 & F4.ppol!=.
replace id4=1 if (F3.ppol-ppol)>=0.1 & F3.ppol!=.
replace id4=1 if (F2.ppol-ppol)>=0.1 & F2.ppol!=.
replace id4=1 if (F.ppol-ppol)>=0.1 & F.ppol!=.



gen id5=0
replace id5=1 if L.id2==1
replace id5=1 if L2.id2==1
replace id5=1 if L3.id2==1
replace id5=1 if L4.id2==1
replace id5=1 if L5.id2==1



gen id6=0
replace id6=1 if (id3==1 | id4==1) & id1==0 & id5==1

rename id6 PolChangeInd1
label variable PolChangeInd1 "Chronic Polarization: Increase of .1 in 5 years, no reversions, min 2 year increase"

drop id1 id2 id3 id4 id5

/////////////////////////////////////////////////////////////////////////////////////////////////
// Polarization Level Indicator

gen id1=0
replace id1=1 if (L5.ppol>=0.4564323  & L4.ppol>=0.4564323  & L3.ppol>=0.4564323  & L2.ppol>=0.4564323  & L1.ppol>=0.4564323 & ppol>=0.4564323) & ///
	(L5.ppol!=. & L4.ppol!=. & L3.ppol!=. & L2.ppol!=. & L1.ppol!=.)

gen id2=0
replace id2=1 if ppol-L.ppol<-0.01
	
gen id3=0
replace id3=1 if L5.id2==1 | L4.id2==1 | L3.id2==1 | L2.id2==1 | L1.id2==1 | id2==1

gen PolLevelInd1=0
replace PolLevelInd1=1 if id1==1 & id3==0
label variable PolLevelInd1 "Elevated Polarization: at 0.4564323 for the previous 5 years, no declines"

drop id1 id2 id3 

gen id1=0
replace id1=1 if (L5.ppol>=0.565625  & L4.ppol>=0.565625  & L3.ppol>=0.565625  & L2.ppol>=0.565625  & L1.ppol>=0.565625 & ppol>=0.565625) & ///
	(L5.ppol!=. & L4.ppol!=. & L3.ppol!=. & L2.ppol!=. & L1.ppol!=.)

gen id2=0
replace id2=1 if ppol-L.ppol<-0.01
	
gen id3=0
replace id3=1 if L5.id2==1 | L4.id2==1 | L3.id2==1 | L2.id2==1 | L1.id2==1 | id2==1

gen PolLevelInd2=0
replace PolLevelInd2=1 if id1==1 & id3==0
label variable PolLevelInd2 "Heightened Polarization: at 0.565625  for the previous 5 years, no declines"

drop id1 id2 id3 

gen id1=0
replace id1=1 if (L5.ppol>=0.721875   & L4.ppol>=0.721875   & L3.ppol>=0.721875   & L2.ppol>=0.721875   & L1.ppol>=0.721875  & ppol>=0.721875 ) & ///
	(L5.ppol!=. & L4.ppol!=. & L3.ppol!=. & L2.ppol!=. & L1.ppol!=.)

gen id2=0
replace id2=1 if ppol-L.ppol<-0.01
	
gen id3=0
replace id3=1 if L5.id2==1 | L4.id2==1 | L3.id2==1 | L2.id2==1 | L1.id2==1 | id2==1

gen PolLevelInd3=0
replace PolLevelInd3=1 if id1==1 & id3==0
label variable PolLevelInd3 "Severe Polarization: at 0.721875  for the previous 5 years, no declines"

drop id1 id2 id3 

keep country year lib ppol regime gdpgrow gdppc inflate gdppcpt libdemtenure demtenure id PolChangeInd1 PolLevelInd1 PolLevelInd2 PolLevelInd3 
save "C:\Users\evanl\Desktop\Polarization\Final Analysis\Analysis 1900 Data.dta", replace
